package com.starbaba.outsideapi.view;

import com.base.common.log.LogService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.outsideapi.model.OutSideApiEntity;
import com.starbaba.outsideapi.service.OutSideApiService;
import com.starbaba.outsideapi.service.impl.OutSideApiServiceImpl;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 外部api查询列表与统计列表
 */

public class OutSideApiAction extends BaseAction implements ModelDriven<OutSideApiEntity> {
	private static final long serialVersionUID = 1L;
	OutSideApiEntity outSideApiEntity=new OutSideApiEntity();
	private OutSideApiService outSideApiService=new OutSideApiServiceImpl();
	
	private String fileName;
	private InputStream input;
	
	public String getFileName() {
		return fileName;
	}

	public InputStream getInput() {
		return input;
	}
	

	//展示页面并且准备数据
	public String showPage(){
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		outSideApiService.showpage(paramsMap);
		return ActionSupport.SUCCESS;
	}
	
	
	//展示合作方列表
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void showOutSideApiList(){
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		List<OutSideApiEntity> list = outSideApiService.showApiList(paramsMap, pageRequest, pageResponse);
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
		}
	}
	
	//展示合作统计页面并且准备数据
		public String showCollaborate(){
			paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
			outSideApiService.showpage(paramsMap);
			return ActionSupport.SUCCESS;
		}
	
	//展示合作方统计列表
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void collaborateList(){
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		List<OutSideApiEntity> list = outSideApiService.showCollaborateList(paramsMap, pageRequest, pageResponse);
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
		}
	}
	
	//导出
	@SuppressWarnings("rawtypes")
	public String exportDetailCVS() throws UnsupportedEncodingException {
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		try {
			pageRequest.setIDisplayStart(-1);
			pageRequest.setIDisplayLength(-1);
			List<OutSideApiEntity> list = outSideApiService.showApiList(paramsMap, pageRequest, pageResponse);
			String value = outSideApiService.exportDetailCVS(list);
			input = new ByteArrayInputStream(value.getBytes("GBK"));
			fileName = System.currentTimeMillis() + ".csv";
		} catch (Exception e) {
		LogService.error(e);
		}
		return ActionSupport.SUCCESS;
	}
	
	
	@Override
	public OutSideApiEntity getModel() {
		return outSideApiEntity;
	}

}
